﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class SelectConnDucts
	{
		public SelectConnDucts(UIDocument uiDocument)
		{
			this.uiDoc = uiDocument;
			this.sel = this.uiDoc.Selection;
			this.ductFilter = new DuctFilter();
		}

		public bool ConnDuctsSelecting(ConnDuctSelectType selectType, ref IList<Duct> connDucts, ref IList<XYZ> connPoints, ref string selPrompt)
		{
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			bool result = true;
			switch (selectType)
			{
			case ConnDuctSelectType.EblowConn:
				selPrompt = this.SelectingEblowConnDucts(ref connDucts, ref connPoints);
				break;
			case ConnDuctSelectType.TeeTConn:
				selPrompt = this.SelectingTStyleTeeConnDucts(ref connDucts, ref connPoints);
				break;
			case ConnDuctSelectType.TeeFConn:
				selPrompt = this.SelectingFStyleTeeConnDucts(ref connDucts, ref connPoints);
				break;
			case ConnDuctSelectType.TeeYConn:
				selPrompt = this.SelectingYStyleTeeConnDucts(ref connDucts, ref connPoints);
				break;
			case ConnDuctSelectType.TeeXConn:
				selPrompt = this.SelectingXStyleTeeConnDucts(ref connDucts, ref connPoints);
				break;
			case ConnDuctSelectType.CrossConn:
				selPrompt = this.SelectingCrossConnDucts(ref connDucts, ref connPoints);
				break;
			case ConnDuctSelectType.CrossXConn:
				selPrompt = this.SelectingXCrossConnDucts(ref connDucts, ref connPoints);
				break;
			case ConnDuctSelectType.TakeOffConn:
				selPrompt = this.SelectingTakeOffConnDucts(ref connDucts, ref connPoints);
				break;
			case ConnDuctSelectType.BackConn:
			case ConnDuctSelectType.ReduceConn:
				selPrompt = this.SelectingReduceBackConnDucts(ref connDucts, ref connPoints, false);
				break;
			case ConnDuctSelectType.RoundConn:
				selPrompt = this.SelectingReduceBackConnDucts(ref connDucts, ref connPoints, true);
				break;
			}
			if (!string.IsNullOrEmpty(selPrompt))
			{
				result = false;
			}
			return result;
		}

		private string SelectingCrossConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			this.selectPrompt = "请选择四通连接需要的风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || (list.Count != 2 && list.Count != 3 && list.Count != 4))
			{
				return "选择风管数目不符合四通连接条件。";
			}
			List<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!this.BeSameSection(list2))
			{
				return this.GetEblowSelectPrompt(7);
			}
			string result = "";
			switch (list2.Count)
			{
			case 2:
				result = this.CheckTwoDuctsConnRelation(ref connDucts, ref connPoints, list2);
				break;
			case 3:
				result = this.CheckThreeDuctsConnRelation(ref connDucts, ref connPoints, list2);
				break;
			case 4:
				result = this.CheckFourDuctsConnRelation(ref connDucts, ref connPoints, list2, true);
				break;
			}
			return result;
		}

		private string SelectingXCrossConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			this.selectPrompt = "请选择四通连接需要的风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || (list.Count != 3 && list.Count != 4))
			{
				return "选择风管数目不符合四通连接条件。";
			}
			List<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!this.BeSameSection(list2))
			{
				return this.GetEblowSelectPrompt(7);
			}
			string result = "";
			int count = list2.Count;
			if (count != 3)
			{
				if (count == 4)
				{
					result = this.CheckFourDuctsConnRelation(ref connDucts, ref connPoints, list2, false);
				}
			}
			else
			{
				this.CheckXStyleCrossConnDucts(connDucts, connPoints, list2);
			}
			return result;
		}

		private void CheckXStyleCrossConnDucts(IList<Duct> connDucts, IList<XYZ> connPoints, IList<Duct> listDuct)
		{
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(listDuct[0].Id);
			specificDuctFilter.ElementIdList.Add(listDuct[1].Id);
			specificDuctFilter.ElementIdList.Add(listDuct[2].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择风管四通连接的主风管……");
			Duct duct = this.uiDoc.GetElementByRef(reference) as Duct;
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
			connDucts.Add(duct);
			connPoints.Add(this.GetFarestPointToPickPoint(reference.GlobalPoint, duct));
			this.RemoveRepeatDuct(ref listDuct, duct);
			connDucts.Add(listDuct[0]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[0]));
			connDucts.Add(listDuct[1]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[1]));
		}

		private string CheckFourDuctsConnRelation(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, IList<Duct> listDuct, bool beParallel = true)
		{
			if (listDuct.Count != 4)
			{
				return "选择风管数目不符合四通连接条件。";
			}
			IList<Duct> list = new List<Duct>();
			IList<Duct> list2 = new List<Duct>();
			this.GroupFourDucts(ref list, ref list2, listDuct, beParallel);
			if (list == null || list2 == null)
			{
				return "风管位置关系不符合连接条件。";
			}
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(list[0].Id);
			specificDuctFilter.ElementIdList.Add(list[1].Id);
			if (beParallel)
			{
				specificDuctFilter.ElementIdList.Add(list2[0].Id);
				specificDuctFilter.ElementIdList.Add(list2[1].Id);
			}
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择风管四通连接的主风管……");
			Duct duct = this.uiDoc.GetElementByRef(reference) as Duct;
			this.RemoveRepeatDuct(ref list, duct);
			this.RemoveRepeatDuct(ref list2, duct);
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
			if (list.Count == 1)
			{
				connDucts.Add(list[0]);
				connPoints.Add(this.GetDuctLineMiddlePoint(list[0]));
				connDucts.Add(list2[0]);
				connPoints.Add(this.GetDuctLineMiddlePoint(list2[0]));
				connDucts.Add(list2[1]);
				connPoints.Add(this.GetDuctLineMiddlePoint(list2[1]));
			}
			else
			{
				connDucts.Add(list2[0]);
				connPoints.Add(this.GetDuctLineMiddlePoint(list2[0]));
				connDucts.Add(list[0]);
				connPoints.Add(this.GetDuctLineMiddlePoint(list[0]));
				connDucts.Add(list[1]);
				connPoints.Add(this.GetDuctLineMiddlePoint(list[1]));
			}
			return "";
		}

		private void GroupFourDucts(ref IList<Duct> firDucts, ref IList<Duct> secDucts, IList<Duct> listDuct, bool beParallel = true)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(listDuct[2]);
			YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(listDuct[3]);
			if (hypiping.IsParallel(hypiping2.Piping))
			{
				if (beParallel)
				{
					if (hypiping3.IsParallel(hypiping4.Piping) && !hypiping4.IsParallel(hypiping2.Piping))
					{
						firDucts.Add(hypiping.Piping);
						firDucts.Add(hypiping2.Piping);
						secDucts.Add(hypiping3.Piping);
						secDucts.Add(hypiping4.Piping);
						return;
					}
				}
				else if (!hypiping3.IsParallel(hypiping4.Piping) && !hypiping4.IsParallel(hypiping.Piping) && !hypiping3.IsParallel(hypiping.Piping))
				{
					firDucts.Add(hypiping.Piping);
					firDucts.Add(hypiping2.Piping);
					secDucts.Add(hypiping3.Piping);
					secDucts.Add(hypiping4.Piping);
					return;
				}
			}
			else if (hypiping.IsParallel(hypiping3.Piping))
			{
				if (beParallel)
				{
					if (hypiping2.IsParallel(hypiping4.Piping) && !hypiping4.IsParallel(hypiping.Piping))
					{
						firDucts.Add(hypiping.Piping);
						firDucts.Add(hypiping3.Piping);
						secDucts.Add(hypiping2.Piping);
						secDucts.Add(hypiping4.Piping);
						return;
					}
				}
				else if (!hypiping2.IsParallel(hypiping4.Piping) && !hypiping4.IsParallel(hypiping.Piping) && !hypiping3.IsParallel(hypiping.Piping))
				{
					firDucts.Add(hypiping.Piping);
					firDucts.Add(hypiping3.Piping);
					secDucts.Add(hypiping2.Piping);
					secDucts.Add(hypiping4.Piping);
					return;
				}
			}
			else if (hypiping.IsParallel(hypiping4.Piping))
			{
				if (beParallel)
				{
					if (hypiping2.IsParallel(hypiping3.Piping) && !hypiping3.IsParallel(hypiping.Piping))
					{
						firDucts.Add(hypiping.Piping);
						firDucts.Add(hypiping4.Piping);
						secDucts.Add(hypiping2.Piping);
						secDucts.Add(hypiping3.Piping);
						return;
					}
				}
				else if (!hypiping2.IsParallel(hypiping3.Piping) && !hypiping3.IsParallel(hypiping.Piping) && !hypiping2.IsParallel(hypiping.Piping))
				{
					firDucts.Add(hypiping.Piping);
					firDucts.Add(hypiping4.Piping);
					secDucts.Add(hypiping2.Piping);
					secDucts.Add(hypiping3.Piping);
					return;
				}
			}
			else if (hypiping2.IsParallel(hypiping3.Piping))
			{
				if (beParallel)
				{
					if (hypiping.IsParallel(hypiping4.Piping) && !hypiping4.IsParallel(hypiping2.Piping))
					{
						firDucts.Add(hypiping2.Piping);
						firDucts.Add(hypiping3.Piping);
						secDucts.Add(hypiping.Piping);
						secDucts.Add(hypiping4.Piping);
						return;
					}
				}
				else if (!hypiping.IsParallel(hypiping4.Piping) && !hypiping4.IsParallel(hypiping2.Piping) && !hypiping.IsParallel(hypiping2.Piping))
				{
					firDucts.Add(hypiping2.Piping);
					firDucts.Add(hypiping3.Piping);
					secDucts.Add(hypiping.Piping);
					secDucts.Add(hypiping4.Piping);
					return;
				}
			}
			else if (hypiping2.IsParallel(hypiping4.Piping))
			{
				if (beParallel)
				{
					if (hypiping.IsParallel(hypiping3.Piping) && !hypiping3.IsParallel(hypiping2.Piping))
					{
						firDucts.Add(hypiping2.Piping);
						firDucts.Add(hypiping4.Piping);
						secDucts.Add(hypiping.Piping);
						secDucts.Add(hypiping3.Piping);
						return;
					}
				}
				else if (!hypiping.IsParallel(hypiping3.Piping) && !hypiping3.IsParallel(hypiping2.Piping) && !hypiping.IsParallel(hypiping2.Piping))
				{
					firDucts.Add(hypiping2.Piping);
					firDucts.Add(hypiping4.Piping);
					secDucts.Add(hypiping.Piping);
					secDucts.Add(hypiping3.Piping);
					return;
				}
			}
			else if (hypiping3.IsParallel(hypiping4.Piping))
			{
				if (beParallel)
				{
					if (hypiping.IsParallel(hypiping2.Piping) && !hypiping2.IsParallel(hypiping3.Piping))
					{
						firDucts.Add(hypiping3.Piping);
						firDucts.Add(hypiping4.Piping);
						secDucts.Add(hypiping.Piping);
						secDucts.Add(hypiping2.Piping);
						return;
					}
				}
				else if (!hypiping.IsParallel(hypiping2.Piping) && !hypiping2.IsParallel(hypiping3.Piping) && !hypiping.IsParallel(hypiping3.Piping))
				{
					firDucts.Add(hypiping3.Piping);
					firDucts.Add(hypiping4.Piping);
					secDucts.Add(hypiping.Piping);
					secDucts.Add(hypiping2.Piping);
					return;
				}
			}
			else
			{
				firDucts = null;
				secDucts = null;
			}
		}

		private string CheckTwoDuctsConnRelation(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, IList<Duct> listDuct)
		{
			if (listDuct.Count != 2)
			{
				return "选择风管数目不符合四通连接条件。";
			}
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			if (!hypiping.IsVertical(hypiping2.Piping))
			{
				return "选择风管位置关系不符合四通连接关系。";
			}
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ dlineP = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
			XYZ dlineP2 = YJKRevitTools.GetDLineP(origin, origin2, origin3, origin4);
			bool flag = hypiping.IsPointOnPipingCurve(dlineP, hypiping.GetDiameter());
			bool flag2 = hypiping2.IsPointOnPipingCurve(dlineP2, hypiping2.GetDiameter());
			if (!flag || !flag2)
			{
				return "选择风管位置关系不符合四通连接关系。";
			}
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(listDuct[0].Id);
			specificDuctFilter.ElementIdList.Add(listDuct[1].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择风管四通连接的主风管……");
			Duct duct = this.uiDoc.GetElementByRef(reference) as Duct;
			connDucts.Add(duct);
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
			connPoints.Add(this.GetDuctLineMiddlePoint(duct));
			YJKPiping<Duct> hypiping3 = (hypiping.Piping.Id.IntegerValue == duct.Id.IntegerValue) ? hypiping2 : hypiping;
			connDucts.Add(hypiping3.Piping);
			connDucts.Add(hypiping3.Piping);
			connPoints.Add(hypiping3.StartConnector.Origin);
			connPoints.Add(hypiping3.EndConnector.Origin);
			return "";
		}

		private string CheckThreeDuctsConnRelation(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, IList<Duct> listDuct)
		{
			if (listDuct.Count != 3)
			{
				return "选择风管数目不符合四通连接条件。";
			}
			Duct duct = null;
			IList<Duct> list = null;
			this.GroupConnThreeDucts(ref duct, ref list, listDuct);
			if (duct == null || list == null)
			{
				return "风管位置关系不符合连接关系。";
			}
			bool flag = this.BeOnDuctIntersectPoint(duct, list[0]);
			bool flag2 = this.BeOnDuctIntersectPoint(duct, list[1]);
			if (!flag || !flag2)
			{
				return "风管位置关系不符合连接关系。";
			}
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(list[0].Id);
			specificDuctFilter.ElementIdList.Add(list[1].Id);
			specificDuctFilter.ElementIdList.Add(duct.Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择风管四通连接的主风管……");
			Duct duct2 = this.uiDoc.GetElementByRef(reference) as Duct;
			if (duct2.Id.IntegerValue == duct.Id.IntegerValue)
			{
				connDucts.Add(duct2);
				connDucts.Add(duct2);
				connPoints.Add(reference.GlobalPoint);
				connPoints.Add(this.GetFarestPointToPickPoint(reference.GlobalPoint, duct2));
				connDucts.Add(list[0]);
				connDucts.Add(list[1]);
				connPoints.Add(this.GetDuctLineMiddlePoint(list[0]));
				connPoints.Add(this.GetDuctLineMiddlePoint(list[1]));
			}
			else
			{
				connDucts.Add(duct2);
				connPoints.Add(reference.GlobalPoint);
				this.RemoveRepeatDuct(ref list, duct2);
				connDucts.Add(list[0]);
				connPoints.Add(this.GetDuctLineMiddlePoint(list[0]));
				connDucts.Add(duct);
				connDucts.Add(duct);
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
				connPoints.Add(hypiping.StartConnector.Origin);
				connPoints.Add(hypiping.EndConnector.Origin);
			}
			return "";
		}

		private bool BeOnDuctIntersectPoint(Duct duct1, Duct duct2)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct1);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(duct2);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ dlineP = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
			return hypiping.IsPointOnPipingCurve(dlineP, hypiping.GetDiameter());
		}

		private string SelectingEblowConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			this.selectPrompt = "请选择弯头连接需要的两根风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || list.Count != 2)
			{
				return this.GetEblowSelectPrompt(1);
			}
			List<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!this.BeSameSection(list2))
			{
				return this.GetEblowSelectPrompt(7);
			}
			int num = this.CheckEblowDuctsRelation(list2);
			if (num == 1 || num == 2)
			{
				return this.GetEblowSelectPrompt(num);
			}
			if (num == 3)
			{
				this.GetPointsOnDucts(ref connPoints, list2);
			}
			else if (num == 4)
			{
				this.GetPointsOnDucts(ref connPoints, list2, num);
			}
			else if (num == 5)
			{
				this.GetPointsOnDucts(ref connPoints, list2, num);
			}
			else
			{
				this.GetPointsOnDucts(ref connPoints, ref connDucts, list2, num);
			}
			if (connDucts == null || connDucts.Count < 1)
			{
				connDucts = list2;
			}
			return "";
		}

		private string SelectingReduceBackConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, bool beRound = false)
		{
			this.selectPrompt = "请选择连接需要的两根风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || list.Count != 2)
			{
				return "连接需要两根风管。";
			}
			List<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!beRound)
			{
				if (!this.BeSameSection(list2))
				{
					return "选择风管截面类型必须相同，且只能是矩形或者圆形风管。";
				}
			}
			else if (!this.BeRightSection(list2))
			{
				return "选择风管截面类型必须为一个为矩形、一个为圆形截面。";
			}
			if (!new YJKPiping<Duct>(list2[0]).IsParallel(list2[1]))
			{
				return "选择的两根风管必须平行。";
			}
			connDucts = list2;
			this.GetPointsOnDucts(ref connPoints, list2);
			return "";
		}

		private string SelectingTakeOffConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			this.selectPrompt = "请选择侧连接需要的两根风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || list.Count != 2)
			{
				return "连接需要两根风管。";
			}
			List<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!this.BeSameSection(list2))
			{
				return this.GetEblowSelectPrompt(7);
			}
			int num = this.CheckTakeOffDuctsRelation(list2);
			string result = "";
			switch (num)
			{
			case 1:
				result = "风管必须为两根，才能进行侧连接。";
				break;
			case 2:
				result = "两根风管不垂直不能进行侧连接。";
				break;
			case 3:
				result = "两根风管位置关系不符合侧连接";
				break;
			case 4:
				connDucts = list2;
				this.GetPointsOnDucts(ref connPoints, list2);
				break;
			case 5:
				this.GetDuctsAndPoints(ref connDucts, ref connPoints, list2);
				break;
			case 6:
				this.GetDuctsAndPoints(list2, ref connDucts, ref connPoints);
				break;
			}
			if (connDucts == null || connDucts.Count < 1)
			{
				connDucts = list2;
			}
			return result;
		}

		private string SelectingFStyleTeeConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			this.selectPrompt = "请选择三通连接需要的风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || (list.Count != 2 && list.Count != 3))
			{
				return "选择风管数量不符合三通连接条件。";
			}
			IList<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!this.BeSameSection(list2))
			{
				return this.GetEblowSelectPrompt(7);
			}
			int num = this.CheckTStyleTeeDuctsRelation(list2);
			string result = "";
			switch (num)
			{
			case 11:
				result = "选择风管数量不符合三通连接条件。";
				break;
			case 12:
				result = "风管不相互垂直不能进行三通连接。";
				break;
			case 13:
				result = "风管位置关系不能進行三通连接。";
				break;
			case 14:
				this.GetOrderTeeConnDucts(ref connDucts, ref connPoints, list2, true);
				break;
			case 15:
				this.GetOrderTeeConnDucts(ref connDucts, ref connPoints, list2, false);
				break;
			case 16:
				this.ReSelectTeeConnDucts(ref connDucts, ref connPoints, list2, true);
				break;
			default:
				switch (num)
				{
				case 31:
					result = "风管位置关系不能進行三通连接。";
					break;
				case 32:
					this.OrderDirectConnTeeDucts(list2, ref connDucts, ref connPoints);
					break;
				case 33:
					this.OrderConnFTeeDucts(list2, ref connDucts, ref connPoints);
					break;
				}
				break;
			}
			return result;
		}

		private string SelectingTStyleTeeConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			this.selectPrompt = "请选择三通连接需要的风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || (list.Count != 2 && list.Count != 3))
			{
				return "选择风管数量不符合三通连接条件。";
			}
			List<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!this.BeSameSection(list2))
			{
				return this.GetEblowSelectPrompt(7);
			}
			int num = this.CheckTStyleTeeDuctsRelation(list2);
			string result = "";
			switch (num)
			{
			case 11:
				result = "选择风管数量不符合三通连接条件。";
				break;
			case 12:
				result = "风管不相互垂直不能进行三通连接。";
				break;
			case 13:
				result = "风管位置关系不能進行三通连接。";
				break;
			case 14:
				this.GetOrderTeeConnDucts(ref connDucts, ref connPoints, list2[0], list2[1]);
				break;
			case 15:
				this.GetOrderTeeConnDucts(ref connDucts, ref connPoints, list2[1], list2[0]);
				break;
			case 16:
				this.ReSelectTeeConnDucts(ref connDucts, ref connPoints, list2);
				break;
			default:
				switch (num)
				{
				case 31:
					result = "风管位置关系不能進行三通连接。";
					break;
				case 32:
					this.OrderDirectConnTeeDucts(ref connDucts, ref connPoints, list2, false);
					break;
				case 33:
					this.OrderDirectConnTeeDucts(ref connDucts, ref connPoints, list2, true);
					break;
				}
				break;
			}
			return result;
		}

		private string SelectingXStyleTeeConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			this.selectPrompt = "请选择三通连接需要的风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || (list.Count != 2 && list.Count != 3))
			{
				return "选择风管数量不符合三通连接条件。";
			}
			List<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!this.BeSameSection(list2))
			{
				return this.GetEblowSelectPrompt(7);
			}
			string result = "";
			if (list2.Count == 2)
			{
				switch (this.CheckTwoDuctsXStyleTeeConn(list2))
				{
				case 21:
					result = "斜风管不支持垂直关系风管连接。";
					break;
				case 22:
					result = "风管位置关系不符合斜三通连接条件。";
					break;
				case 23:
					this.GetOrderTeeConnDucts(ref connDucts, ref connPoints, list2[0], list2[1]);
					break;
				case 24:
					this.GetOrderTeeConnDucts(ref connDucts, ref connPoints, list2[1], list2[0]);
					break;
				case 25:
					this.ReSelectTeeConnDucts(ref connDucts, ref connPoints, list2);
					break;
				}
			}
			else
			{
				Duct angleDuct = null;
				IList<Duct> list3 = null;
				this.GroupXStyleConnDucts(ref angleDuct, ref list3, list2);
				if (list3 == null)
				{
					result = "风管位置关系不符合斜三通连接条件。";
				}
				else
				{
					bool beOnDuct = this.BeIntersectPoint(angleDuct, list3);
					this.OrderXStyleDucts(angleDuct, list3, beOnDuct, ref connDucts, ref connPoints);
				}
			}
			return result;
		}

		private string SelectingYStyleTeeConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			this.selectPrompt = "请选择三通连接需要的风管……";
			connDucts = new List<Duct>();
			connPoints = new List<XYZ>();
			IList<Element> list = this.sel.PickElementsByRectangle(this.ductFilter, this.selectPrompt);
			if (list == null || list.Count != 3)
			{
				return "选择风管数量不符合三通连接条件。";
			}
			IList<Duct> list2 = new List<Duct>();
			foreach (Element element in list)
			{
				list2.Add(element as Duct);
			}
			if (!this.BeSameSection(list2))
			{
				return this.GetEblowSelectPrompt(7);
			}
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(list2[0].Id);
			specificDuctFilter.ElementIdList.Add(list2[1].Id);
			specificDuctFilter.ElementIdList.Add(list2[2].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接进口管道……");
			Duct duct = this.uiDoc.Document.GetElementByRef(reference) as Duct;
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
			this.RemoveRepeatDuct(ref list2, duct);
			connDucts.Add(list2[0]);
			connPoints.Add(this.GetDuctLineMiddlePoint(list2[0]));
			connDucts.Add(list2[1]);
			connPoints.Add(this.GetDuctLineMiddlePoint(list2[1]));
			return "";
		}

		private void OrderXStyleDucts(Duct angleDuct, IList<Duct> groupedDuct, bool beOnDuct, ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(groupedDuct[0].Id);
			specificDuctFilter.ElementIdList.Add(groupedDuct[1].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接进口管道……");
			Duct duct = this.uiDoc.Document.GetElementByRef(reference) as Duct;
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
			this.RemoveRepeatDuct(ref groupedDuct, duct);
			connDucts.Add(groupedDuct[0]);
			connPoints.Add(this.GetDuctLineMiddlePoint(groupedDuct[0]));
			if (beOnDuct)
			{
				specificDuctFilter.ElementIdList = new List<ElementId>();
				specificDuctFilter.ElementIdList.Add(angleDuct.Id);
				reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接支管道……");
				duct = (this.uiDoc.Document.GetElementByRef(reference) as Duct);
				connDucts.Add(duct);
				connPoints.Add(reference.GlobalPoint);
				return;
			}
			connDucts.Add(angleDuct);
			connPoints.Add(this.GetDuctLineMiddlePoint(angleDuct));
		}

		private bool BeIntersectPoint(Duct angleDuct, IList<Duct> groupedDuct)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(angleDuct);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(groupedDuct[0]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(groupedDuct[1]);
			XYZ origin = hypiping2.StartConnector.Origin;
			XYZ origin2 = hypiping2.EndConnector.Origin;
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ origin5 = hypiping3.StartConnector.Origin;
			XYZ origin6 = hypiping3.EndConnector.Origin;
			XYZ dlineP = YJKRevitTools.GetDLineP(origin5, origin6, origin, origin2);
			XYZ dlineP2 = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
			bool flag = hypiping.IsPointOnPipingCurve(dlineP, hypiping.GetDiameter());
			bool flag2 = hypiping.IsPointOnPipingCurve(dlineP2, hypiping.GetDiameter());
			return flag || flag2;
		}

		private int CheckTwoDuctsXStyleTeeConn(IList<Duct> listDuct)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			if (hypiping.IsVertical(listDuct[1]))
			{
				return 21;
			}
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ dlineP = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
			XYZ dlineP2 = YJKRevitTools.GetDLineP(origin, origin2, origin3, origin4);
			bool flag = hypiping.IsPointOnPipingCurve(dlineP, hypiping.GetDiameter());
			bool flag2 = hypiping2.IsPointOnPipingCurve(dlineP2, hypiping2.GetDiameter());
			if (!flag && !flag2)
			{
				return 22;
			}
			if (flag && !flag2)
			{
				return 23;
			}
			if (!flag && flag2)
			{
				return 24;
			}
			return 25;
		}

		private void GroupXStyleConnDucts(ref Duct duct, ref IList<Duct> groupedDuct, IList<Duct> listDuct)
		{
			groupedDuct = new List<Duct>();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(listDuct[2]);
			if (hypiping.IsParallel(hypiping2.Piping) && !hypiping.IsParallel(hypiping3.Piping) && !hypiping3.IsVertical(hypiping.Piping))
			{
				duct = hypiping3.Piping;
				groupedDuct.Add(hypiping.Piping);
				groupedDuct.Add(hypiping2.Piping);
				return;
			}
			if (hypiping.IsParallel(hypiping3.Piping) && !hypiping.IsParallel(hypiping2.Piping) && !hypiping2.IsVertical(hypiping.Piping))
			{
				duct = hypiping2.Piping;
				groupedDuct.Add(hypiping.Piping);
				groupedDuct.Add(hypiping3.Piping);
				return;
			}
			if (hypiping2.IsParallel(hypiping3.Piping) && !hypiping.IsParallel(hypiping2.Piping) && !hypiping2.IsVertical(hypiping.Piping))
			{
				duct = hypiping.Piping;
				groupedDuct.Add(hypiping2.Piping);
				groupedDuct.Add(hypiping3.Piping);
				return;
			}
			duct = null;
			groupedDuct = null;
		}

		private void GroupConnThreeDucts(ref Duct duct, ref IList<Duct> groupedDuct, IList<Duct> listDuct)
		{
			groupedDuct = new List<Duct>();
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(listDuct[2]);
			if (hypiping.IsParallel(hypiping2.Piping) && hypiping3.IsVertical(hypiping.Piping))
			{
				duct = hypiping3.Piping;
				groupedDuct.Add(hypiping.Piping);
				groupedDuct.Add(hypiping2.Piping);
				return;
			}
			if (hypiping.IsParallel(hypiping3.Piping) && hypiping2.IsVertical(hypiping.Piping))
			{
				duct = hypiping2.Piping;
				groupedDuct.Add(hypiping.Piping);
				groupedDuct.Add(hypiping3.Piping);
				return;
			}
			if (hypiping2.IsParallel(hypiping3.Piping) && hypiping2.IsVertical(hypiping.Piping))
			{
				duct = hypiping.Piping;
				groupedDuct.Add(hypiping2.Piping);
				groupedDuct.Add(hypiping3.Piping);
				return;
			}
			duct = null;
			groupedDuct = null;
		}

		private void OrderDirectConnTeeDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, IList<Duct> listDuct, bool beReSelect = false)
		{
			Duct verticalDuctTeeDucts = this.GetVerticalDuctTeeDucts(listDuct);
			this.RemoveRepeatDuct(ref listDuct, verticalDuctTeeDucts);
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(listDuct[0].Id);
			specificDuctFilter.ElementIdList.Add(listDuct[1].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接进口风管……");
			Duct duct = this.uiDoc.GetElementByRef(reference) as Duct;
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
			this.RemoveRepeatDuct(ref listDuct, duct);
			connDucts.Add(listDuct[0]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[0]));
			connDucts.Add(verticalDuctTeeDucts);
			if (beReSelect)
			{
				specificDuctFilter.ElementIdList = new List<ElementId>();
				specificDuctFilter.ElementIdList.Add(verticalDuctTeeDucts.Id);
				reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接支风管……");
				connPoints.Add(reference.GlobalPoint);
				return;
			}
			connPoints.Add(this.GetDuctLineMiddlePoint(verticalDuctTeeDucts));
		}

		private void RemoveRepeatDuct(ref IList<Duct> listDuct, Duct duct)
		{
			List<Duct> list = new List<Duct>();
			foreach (Duct duct2 in listDuct)
			{
				if (duct2.Id.IntegerValue != duct.Id.IntegerValue)
				{
					list.Add(duct2);
				}
			}
			listDuct = list;
		}

		private void OrderConnFTeeDucts(IList<Duct> listDuct, ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			Duct verticalDuctTeeDucts = this.GetVerticalDuctTeeDucts(listDuct);
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(verticalDuctTeeDucts.Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接进口风管端……");
			Duct item = this.uiDoc.GetElementByRef(reference) as Duct;
			connDucts.Add(item);
			connPoints.Add(reference.GlobalPoint);
			this.RemoveRepeatDuct(ref listDuct, verticalDuctTeeDucts);
			connDucts.Add(listDuct[0]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[0]));
			connDucts.Add(listDuct[1]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[1]));
		}

		private void OrderDirectConnTeeDucts(IList<Duct> listDuct, ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			Duct verticalDuctTeeDucts = this.GetVerticalDuctTeeDucts(listDuct);
			connDucts.Add(verticalDuctTeeDucts);
			connPoints.Add(this.GetDuctLineMiddlePoint(verticalDuctTeeDucts));
			this.RemoveRepeatDuct(ref listDuct, verticalDuctTeeDucts);
			connDucts.Add(listDuct[0]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[0]));
			connDucts.Add(listDuct[1]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[1]));
		}

		private Duct GetVerticalDuctTeeDucts(IList<Duct> listDuct)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(listDuct[2]);
			Duct piping;
			if (hypiping.IsVertical(hypiping2.Piping) && hypiping.IsVertical(hypiping3.Piping))
			{
				piping = hypiping.Piping;
			}
			else if (hypiping2.IsVertical(hypiping.Piping) && hypiping2.IsVertical(hypiping3.Piping))
			{
				piping = hypiping2.Piping;
			}
			else
			{
				piping = hypiping3.Piping;
			}
			return piping;
		}

		private void ReSelectTeeConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, List<Duct> listDuct)
		{
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(listDuct[0].Id);
			specificDuctFilter.ElementIdList.Add(listDuct[1].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接进口风管端……");
			Duct duct = this.uiDoc.Document.GetElementByRef(reference) as Duct;
			connDucts.Add(duct);
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
			connPoints.Add(this.GetFarestPointToPickPoint(reference.GlobalPoint, duct));
			specificDuctFilter.ElementIdList.Remove(duct.Id);
			reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接支风管端……");
			duct = (this.uiDoc.Document.GetElementByRef(reference) as Duct);
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
		}

		private void ReSelectTeeConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, IList<Duct> listDuct, bool beFStyle)
		{
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(listDuct[0].Id);
			specificDuctFilter.ElementIdList.Add(listDuct[1].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接进口风管端……");
			Duct duct = this.uiDoc.Document.GetElementByRef(reference) as Duct;
			connDucts.Add(duct);
			connPoints.Add(reference.GlobalPoint);
			this.RemoveRepeatDuct(ref listDuct, duct);
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			connDucts.Add(listDuct[0]);
			connDucts.Add(listDuct[0]);
			connPoints.Add(hypiping.StartConnector.Origin);
			connPoints.Add(hypiping.EndConnector.Origin);
		}

		private void GetOrderTeeConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, Duct duct1, Duct duct2)
		{
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(duct1.Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "请选择三通连接进口管道端……");
			connDucts.Add(duct1);
			connDucts.Add(duct1);
			connDucts.Add(duct2);
			connPoints.Add(reference.GlobalPoint);
			connPoints.Add(this.GetFarestPointToPickPoint(reference.GlobalPoint, duct1));
			connPoints.Add(this.GetDuctLineMiddlePoint(duct2));
		}

		private void GetOrderTeeConnDucts(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, IList<Duct> listDuct, bool beFirst)
		{
			if (beFirst)
			{
				connDucts.Add(listDuct[1]);
				connDucts.Add(listDuct[0]);
				connDucts.Add(listDuct[0]);
				connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[1]));
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
				connPoints.Add(hypiping.StartConnector.Origin);
				connPoints.Add(hypiping.EndConnector.Origin);
				return;
			}
			connDucts.Add(listDuct[0]);
			connDucts.Add(listDuct[1]);
			connDucts.Add(listDuct[1]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[0]));
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			connPoints.Add(hypiping2.StartConnector.Origin);
			connPoints.Add(hypiping2.EndConnector.Origin);
		}

		private XYZ GetFarestPointToPickPoint(XYZ pickPoint, Duct duct)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			if (origin.DistanceTo(pickPoint) <= origin2.DistanceTo(pickPoint))
			{
				return origin;
			}
			return origin2;
		}

		private int CheckTStyleTeeDuctsRelation(IList<Duct> listDuct)
		{
			if (listDuct == null || (listDuct.Count != 2 && listDuct.Count != 3))
			{
				return 11;
			}
			int result = 11;
			if (listDuct.Count == 2)
			{
				switch (this.CheckTakeOffDuctsRelation(listDuct))
				{
				case 1:
					result = 11;
					break;
				case 2:
					result = 12;
					break;
				case 3:
					result = 13;
					break;
				case 4:
					result = 14;
					break;
				case 5:
					result = 15;
					break;
				case 6:
					result = 16;
					break;
				}
			}
			else
			{
				result = this.CheckThreeDuctsRelation(listDuct);
			}
			return result;
		}

		private int CheckThreeDuctsRelation(IList<Duct> listDuct)
		{
			if (listDuct == null || listDuct.Count != 3)
			{
				return 11;
			}
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(listDuct[2]);
			int result;
			if (hypiping.IsVertical(hypiping2.Piping) && hypiping2.IsParallel(hypiping3.Piping))
			{
				result = this.CheckThreeDuctsRelation(hypiping, hypiping2, hypiping3);
			}
			else if (hypiping.IsParallel(hypiping2.Piping) && hypiping3.IsVertical(hypiping.Piping))
			{
				result = this.CheckThreeDuctsRelation(hypiping3, hypiping, hypiping2);
			}
			else if (hypiping.IsParallel(hypiping3.Piping) && hypiping2.IsVertical(hypiping.Piping))
			{
				result = this.CheckThreeDuctsRelation(hypiping2, hypiping3, hypiping);
			}
			else
			{
				result = 31;
			}
			return result;
		}

		private int CheckThreeDuctsRelation(YJKPiping<Duct> hyDuct1, YJKPiping<Duct> hyDuct2, YJKPiping<Duct> hyDuct3)
		{
			IList<Duct> list = new List<Duct>();
			list.Add(hyDuct1.Piping);
			list.Add(hyDuct2.Piping);
			IList<Duct> list2 = new List<Duct>();
			list2.Add(hyDuct1.Piping);
			list2.Add(hyDuct3.Piping);
			int num = this.CheckTakeOffDuctsRelation(list);
			int num2 = this.CheckTakeOffDuctsRelation(list2);
			int result;
			if (num == 3 && num2 == 3)
			{
				result = 32;
			}
			else
			{
				result = 33;
			}
			return result;
		}

		private void GetDuctsAndPoints(IList<Duct> listDuct, ref IList<Duct> connDucts, ref IList<XYZ> connPoints)
		{
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(listDuct[0].Id);
			specificDuctFilter.ElementIdList.Add(listDuct[1].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "选择侧连接风管的主风管……");
			connDucts.Add(this.uiDoc.Document.GetElementByRef(reference) as Duct);
			connPoints.Add(reference.GlobalPoint);
			specificDuctFilter.ElementIdList.Remove(connDucts[0].Id);
			reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, "选择侧连接风管支管的保留端……");
			connDucts.Add(this.uiDoc.Document.GetElementByRef(reference) as Duct);
			connPoints.Add(reference.GlobalPoint);
		}

		private void GetDuctsAndPoints(ref IList<Duct> connDucts, ref IList<XYZ> connPoints, IList<Duct> listDuct)
		{
			connDucts.Add(listDuct[1]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[1]));
			connDucts.Add(listDuct[0]);
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[0]));
		}

		private int CheckTakeOffDuctsRelation(IList<Duct> listDuct)
		{
			if (listDuct == null || listDuct.Count != 2)
			{
				return 1;
			}
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			if (!hypiping.IsVertical(listDuct[1]))
			{
				return 2;
			}
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ dlineP = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
			XYZ dlineP2 = YJKRevitTools.GetDLineP(origin, origin2, origin3, origin4);
			bool flag = hypiping.IsPointOnPipingCurve(dlineP, hypiping.GetDiameter());
			bool flag2 = hypiping2.IsPointOnPipingCurve(dlineP2, hypiping2.GetDiameter());
			if (!flag && !flag2)
			{
				return 3;
			}
			if (flag && !flag2)
			{
				return 4;
			}
			if (!flag && flag2)
			{
				return 5;
			}
			return 6;
		}

		private void GetPointsOnDucts(ref IList<XYZ> connPoints, ref IList<Duct> connDucts, List<Duct> listDuct, int checkTypeNum)
		{
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			specificDuctFilter.ElementIdList.Add(listDuct[0].Id);
			specificDuctFilter.ElementIdList.Add(listDuct[1].Id);
			Reference reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, this.GetEblowSelectPrompt(checkTypeNum));
			connDucts.Add(this.uiDoc.Document.GetElementByRef(reference) as Duct);
			connPoints.Add(reference.GlobalPoint);
			reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, this.GetEblowSelectPrompt(checkTypeNum));
			connDucts.Add(this.uiDoc.Document.GetElementByRef(reference) as Duct);
			connPoints.Add(reference.GlobalPoint);
		}

		private void GetPointsOnDucts(ref IList<XYZ> connPoints, List<Duct> listDuct, int checkTypeNum)
		{
			SpecificDuctFilter specificDuctFilter = new SpecificDuctFilter();
			specificDuctFilter.ElementIdList = new List<ElementId>();
			Reference reference;
			if (checkTypeNum == 4)
			{
				specificDuctFilter.ElementIdList.Add(listDuct[0].Id);
				reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, this.GetEblowSelectPrompt(checkTypeNum));
				connPoints.Add(reference.GlobalPoint);
				connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[1]));
				return;
			}
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[0]));
			specificDuctFilter.ElementIdList.Add(listDuct[1].Id);
			reference = this.sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, specificDuctFilter, this.GetEblowSelectPrompt(checkTypeNum));
			connPoints.Add(reference.GlobalPoint);
		}

		private void GetPointsOnDucts(ref IList<XYZ> connPoints, List<Duct> listDuct)
		{
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[0]));
			connPoints.Add(this.GetDuctLineMiddlePoint(listDuct[1]));
		}

		private XYZ GetDuctLineMiddlePoint(Duct duct)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			return hypiping.StartConnector.Origin.Add(hypiping.EndConnector.Origin) / 2.0;
		}

		private int CheckEblowDuctsRelation(IList<Duct> listDuct)
		{
			if (listDuct == null || listDuct.Count != 2)
			{
				return 1;
			}
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(listDuct[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(listDuct[1]);
			if (hypiping.IsParallel(hypiping2.Piping))
			{
				return 2;
			}
			XYZ origin = hypiping.StartConnector.Origin;
			XYZ origin2 = hypiping.EndConnector.Origin;
			XYZ origin3 = hypiping2.StartConnector.Origin;
			XYZ origin4 = hypiping2.EndConnector.Origin;
			XYZ dlineP = YJKRevitTools.GetDLineP(origin3, origin4, origin, origin2);
			XYZ dlineP2 = YJKRevitTools.GetDLineP(origin, origin2, origin3, origin4);
			bool flag = hypiping.IsPointOnPipingCurve(dlineP, hypiping.GetDiameter());
			bool flag2 = hypiping2.IsPointOnPipingCurve(dlineP2, hypiping2.GetDiameter());
			if (!flag && !flag2)
			{
				return 3;
			}
			if (flag && !flag2)
			{
				return 4;
			}
			if (!flag && flag2)
			{
				return 5;
			}
			return 6;
		}

		private string GetEblowSelectPrompt(int promptTypeNum)
		{
			string result = "";
			switch (promptTypeNum)
			{
			case 1:
				result = "风管必须为两根，才能进行弯头连接。";
				break;
			case 2:
				result = "平行风管不能进行弯头连接。";
				break;
			case 3:
				result = "风管可以直接进行弯头连接。";
				break;
			case 4:
				result = "请选择相交风管连接弯头，保留部分风管……";
				break;
			case 5:
				result = "请选择相交风管连接弯头，保留部分风管……";
				break;
			case 6:
				result = "请分别选择弯头连接需要保留部分风管段……";
				break;
			case 7:
				result = "选择风管的截面必须相同，且应为圆形或矩形截面。";
				break;
			}
			return result;
		}

		private bool BeSameSection(IList<Duct> listDuct)
		{
			if (listDuct == null || listDuct.Count < 1)
			{
				return false;
			}
			string ductSectionName = this.GetDuctSectionName(listDuct[0]);
			if (ductSectionName != "矩形" && ductSectionName != "圆形")
			{
				return false;
			}
			for (int i = 1; i < listDuct.Count; i++)
			{
				Duct duct = listDuct[i];
				string ductSectionName2 = this.GetDuctSectionName(duct);
				if (ductSectionName != ductSectionName2)
				{
					return false;
				}
			}
			return true;
		}

		private bool BeRightSection(IList<Duct> listDuct)
		{
			if (listDuct == null || listDuct.Count != 2)
			{
				return false;
			}
			string ductSectionName = this.GetDuctSectionName(listDuct[0]);
			string ductSectionName2 = this.GetDuctSectionName(listDuct[1]);
			return (!(ductSectionName != "矩形") || !(ductSectionName != "圆形")) && (!(ductSectionName2 != "矩形") || !(ductSectionName2 != "圆形")) && !(ductSectionName == ductSectionName2);
		}

		private string GetDuctSectionName(Duct duct)
		{
			return YJKRevitTools.GetConnectorSharpName(new YJKPiping<Duct>(duct).StartConnector.Connector);
		}

		private UIDocument uiDoc;

		private Selection sel;

		private ISelectionFilter ductFilter;

		private string selectPrompt;
	}
}
